<?php

namespace App\Models;

use Encore\Admin\Traits\AdminBuilder;
use Encore\Admin\Traits\ModelTree;
use Illuminate\Database\Eloquent\Model;

class Column extends Model
{
    //
    use ModelTree, AdminBuilder;

    protected $casts = [
        'state' => 'json',
    ];

    public function __construct(array $attributes = [])
    {
        parent::__construct($attributes);

    }

    public function sites()
    {
        return $this->hasOne('App\Models\Site');
    }

    public function parent()
    {
        return $this->belongsTo('App\Model\Column', 'parent_id', 'id');
    }

    public function children()
    {
        return $this->hasMany('App\Models\Column', 'parent_id', 'id');
    }

    public function indexChildren($id)
    {
        return $this->where('parent_id',$id)->where('state','like','%"1"%')->select('id','title')->get();
    }
    public function indexSite($id)
    {
        $ids    = $this->where('parent_id',$id)->where('state','like','%"1"%')->pluck('id')->toArray();
        return Site::whereIn('column_id',$ids)->where('state',1)->where('is_top',1)->select('id','name')->orderBy('views', 'desc')->take(13)->get();
    }
}